package adapters;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sxpjyy.monkey.mobilenurse.R;

import java.text.DecimalFormat;
import java.util.List;

import mytools.Trans2RMB;
import objects.CostItem;


/**
 * A placeholder fragment containing a simple view.
 */
public class CostPlaceholderFragment extends Fragment {

    private Context mContext;
    private static List<CostItem> costItemList;

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private final String ARG_SECTION_NUMBER = "section_number";

    public CostPlaceholderFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public void setList(List<CostItem> list) {
        costItemList = list;
    }

    private String getTotalNum() {
        double total = 0;
        String result;
        if (costItemList != null && costItemList.size() > 0) {
            for (CostItem costItem : costItemList
                    ) {
                total += Double.valueOf(costItem.getCost_quantity());
            }
            total = (total * 1000 + 0.5) / 1000;
            // 将 result 进行格式化
            result = new DecimalFormat("##0.00").format(total);
            return String.valueOf(result);
        }
        return null;
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public CostPlaceholderFragment newInstance(int sectionNumber) {
        CostPlaceholderFragment fragment = new CostPlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {
        int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
        View rootView;
        switch (sectionNumber) {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_cost_1, parent, false);
                ListView listView = (ListView) rootView.findViewById(R.id.cost_item_list);
                TextView textNum = (TextView) rootView.findViewById(R.id.total_num_text);
                TextView textChs = (TextView) rootView.findViewById(R.id.total_chs_text);
                final CostItemAdapter costItemAdapter = new CostItemAdapter(mContext);
                listView.setAdapter(costItemAdapter);
                if (costItemList != null && costItemList.size() > 0) {
                    costItemAdapter.setList(costItemList);
                    costItemAdapter.notifyDataSetChanged();
                }
                String total = getTotalNum();
                if (total != null && !total.equals("")) {
                    textNum.setText(total);
                    textChs.setText(Trans2RMB.getRMB(total));
                }
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        Toast.makeText(mContext, costItemList.get(position).getCost_name() + "clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
//                case 2:
//                    break;
//                case 3:
//                    break;
            default:
                rootView = inflater.inflate(R.layout.fragment_money, parent, false);
                TextView textView = (TextView) rootView.findViewById(R.id.section_label);
                textView.setText(getString(R.string.section_format, sectionNumber));
                break;
        }
        return rootView;
    }
}